<?php


class UserInfoService extends TP_Service {
    private $model;
    private $role;
    public function __construct() {
        parent::__construct();
        $this->load->model("SModel/Staff_model");
        $this->load->model("SModel/Customer_model");
        $this->load->service("PService/UploadService");
        $this->whichModel();
    }


    private function whichModel() {
        $this->role = $this->getRole($_SESSION[SESS_UUID]);
        if ($this->role->account_type == AccountType::Admin[TPEnum::Value]) {
            $this->model = $this->Staff_model;
        } else {
            $this->model = $this->Customer_model;
        }
    }


    /**
     * 方法名 headImg
     *
     * @Name 获取方法列和角色方法列
     * @Desc 修改个人头像
     * @OpenFlag 是否开放,1开放0权限控制
     * @Author      zn
     * @Date 2021-11-15 09:30:37 星期一
     * @return      [type]                   [description]
     */
    public function headImg($id) {
        $res = $this->UploadService->upload_img('headImg', 1044480);
        if (empty($res)) {
            exit(retJson(ErrCode::ErrCode_10601));
        }

        $this->model->update(['head_img' => $res['path']], ['id' => $id]);
        return $res;
    }



    /**
     * 方法名 userInfo
     *
     * @Name 获取方法列和角色方法列
     * @Desc 个人信息
     * @OpenFlag 是否开放,1开放0权限控制
     * @Author      zn
     * @Date 2021-11-15 09:30:37 星期一
     * @return      [type]                   [description]
     */
    public function userInfo() {
        if ($this->role->account_type == AccountType::Admin[TPEnum::Value]) {
            $conarr = array(
                'items' => "tp_staff.id, head_img, usercode, email, username, mobile, department, organization, position,ascription,ascription_name,manager,manager_name",
                'join'  => array("tp_account a", 'tp_staff.aid = a.id'),
            );
        } else {
            $conarr = array(
                'items' => "tp_customer.id, head_img, usercode, email, username, mobile,othermobile, country, region, company, position, ascription,ascription_name",
                'join'  => array("tp_account a", 'tp_customer.aid = a.id'),
            );
        }

        $conarr['where'] = sql_format("uuid = '{0}'", $_SESSION[SESS_UUID]);
        $res = $this->model->get_single_full($conarr);
        return $res ?? [];
    }



    /**
     * 方法名 userInfo
     *
     * @Name 获取方法列和角色方法列
     * @Desc 个人信息修改
     * @OpenFlag 是否开放,1开放0权限控制
     * @Author      zn
     * @Date 2021-11-15 09:30:37 星期一
     * @return      [type]                   [description]
     */
    public function updateInfo($param) {
        if ($this->role->account_type == AccountType::Admin[TPEnum::Value]) {
            if (!isset($param['id'], $param['email'], $param['username'], $param['department'], $param['organization'], $param['position'])) {
                exit(retJson(ErrCode::ErrCode_10002));
            }

            if (!filter_var($param['email'], FILTER_VALIDATE_EMAIL)) {
                exit(retJson(ErrCode::ErrCode_10504));
            }

            $sql = sql_format(
                "UPDATE tp_staff s JOIN tp_account a ON s.aid = a.id SET
                email = '{0}', username = '{1}', mobile = '{2}', department = '{3}', organization = '{4}', position = '{5}',ascription = '{7}',ascription_name = '{8}', manager = '{9}',manager_name = '{10}' WHERE s.id = '{6}'",
                $param['email'],
                $param['username'],
                $param['mobile'],
                $param['department'],
                $param['organization'],
                $param['position'],
                $param['id'],
                $param['ascription'] ?? '',
                $param['ascription_name'] ?? '',
                $param['manager'] ?? '',
                $param['manager_name'] ?? '',
            );
        } else {
            if (!isset($param['id'], $param['username'], $param['country'], $param['region'], $param['position'])) {
                exit(retJson(ErrCode::ErrCode_10002));
            }

            $sql = sql_format(
                "UPDATE tp_customer c JOIN tp_account a ON c.aid = a.id SET
                username = '{0}', country = '{1}', region = '{2}', mobile = '{3}', position = '{4}' ,c.othermobile = '{6}' WHERE c.id = '{5}'",
                $param['username'],
                $param['country'],
                $param['region'],
                $param['mobile'] ?? '',
                $param['position'],
                $param['id'],
                $param['othermobile'] ?? '',
            );
        }



        // if (!validMobile($param['mobile'])) {
        //     exit(retJson(ErrCode::ErrCode_10505));
        // }

        $this->model->execute($sql);
        return;
    }
}
